{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Measurements Kata\n",
    "\n",
    "The **Measurements** quantum kata is a series of exercises designed\n",
    "to get you familiar with the concept of measurements and with programming in Q#.\n",
    "It covers the following topics:\n",
    "* single-qubit measurements,\n",
    "* discriminating orthogonal and nonorthogonal states.\n",
    "\n",
    "A detailed introduction into single-qubit measurements work can be found in [this tutorial](../tutorials/SingleQubitSystemMeasurements/SingleQubitSystemMeasurements.ipynb).\n",
    "\n",
    "Each task is wrapped in one operation preceded by the description of the task.\n",
    "Your goal is to fill in the blank (marked with `// ...` comments)\n",
    "with some Q# code that solves the task. To verify your answer, run the cell using Ctrl+Enter (⌘+Enter on macOS).\n",
    "\n",
    "The tasks are given in approximate order of increasing difficulty; harder ones are marked with asterisks."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Part I. Discriminating Orthogonal States"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Task 1.1. $|0\\rangle$ or $|1\\rangle$?\n",
    "\n",
    "**Input:** A qubit which is guaranteed to be in either the $|0\\rangle$ or the $|1\\rangle$ state.\n",
    "\n",
    "**Output:**  `true` if the qubit was in the $|1\\rangle$ state, or `false` if it was in the $|0\\rangle$ state. The state of the qubit at the end of the operation does not matter."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%kata T101_IsQubitOne \n",
    "\n",
    "operation IsQubitOne (q : Qubit) : Bool {\n",
    "    // The operation M will measure a qubit in the Z basis (|0⟩ and |1⟩ basis)\n",
    "    // and return Zero if the observed state was |0⟩ or One if the state was |1⟩.\n",
    "    // To answer the question, you need to perform the measurement and check whether the result\n",
    "    // equals One - either directly or using library function IsResultOne.\n",
    "    //\n",
    "    // Type the following: return M(q) == One;\n",
    "    // Then run the cell using Ctrl+Enter (⌘+Enter on macOS).\n",
    "\n",
    "    // ...\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Can't come up with a solution? See the explained solution in the [Measurements Workbook](./Workbook_Measurements.ipynb#Task-1.1.-$|0\\rangle$-or-$|1\\rangle$?).*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Task 1.2. Set the qubit to the $|0\\rangle$ state.\n",
    "\n",
    "**Input:** A qubit in an arbitrary state.\n",
    "\n",
    "**Goal:**  Change the state of the qubit to $|0\\rangle$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%kata T102_InitializeQubit \n",
    "\n",
    "operation InitializeQubit (q : Qubit) : Unit {\n",
    "    // ...\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Can't come up with a solution? See the explained solution in the [Measurements Workbook](./Workbook_Measurements.ipynb#Task-1.2.-Set-the-qubit-to-the-$|0\\rangle$-state.).*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Task 1.3. $|+\\rangle$ or $|-\\rangle$?\n",
    "\n",
    "**Input:** A qubit which is guaranteed to be in either the $|+\\rangle$ or the $|-\\rangle$ state. As a reminder, $|+\\rangle = \\frac{1}{\\sqrt{2}} \\big(|0\\rangle + |1\\rangle\\big)$, $|-\\rangle = \\frac{1}{\\sqrt{2}} \\big(|0\\rangle - |1\\rangle\\big)$.\n",
    "\n",
    "**Output:** `true` if the qubit was in the $|+\\rangle$ state, or `false` if it was in the $|-\\rangle$ state. The state of the qubit at the end of the operation does not matter."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%kata T103_IsQubitPlus \n",
    "\n",
    "operation IsQubitPlus (q : Qubit) : Bool {\n",
    "    // ...\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Can't come up with a solution? See the explained solution in the [Measurements Workbook](./Workbook_Measurements.ipynb#Task-1.3.-$|+\\rangle$-or-$|-\\rangle$?).*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Task 1.4. $|A\\rangle$ or $|B\\rangle$?\n",
    "\n",
    "**Inputs:** \n",
    "\n",
    "1. Angle $\\alpha$, in radians, represented as a `Double`.\n",
    "2. A qubit which is guaranteed to be in either the $|A\\rangle$ or the $|B\\rangle$ state, where $|A\\rangle = \\cos \\alpha |0\\rangle + \\sin \\alpha |1\\rangle$ and $|B\\rangle = - \\sin \\alpha |0\\rangle + \\cos \\alpha |1\\rangle$.\n",
    "\n",
    "**Output:** `true` if the qubit was in the $|A\\rangle$ state, or `false` if it was in the $|B\\rangle$ state. The state of the qubit at the end of the operation does not matter."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%kata T104_IsQubitA\n",
    "\n",
    "operation IsQubitA (alpha : Double, q : Qubit) : Bool {\n",
    "    // ...\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Can't come up with a solution? See the explained solution in the [Measurements Workbook](./Workbook_Measurements.ipynb#Task-1.4.-$|A\\rangle$-or-$|B\\rangle$?).*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Task 1.5. $|00\\rangle$ or $|11\\rangle$?\n",
    "\n",
    "**Input:** Two qubits (stored in an array of length 2) which are guaranteed to be in either the $|00\\rangle$ or the $|11\\rangle$ state.\n",
    "\n",
    "**Output:** 0 if the qubits were in the $|00\\rangle$ state, or 1 if they were in the $|11\\rangle$ state. The state of the qubits at the end of the operation does not matter."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%kata T105_ZeroZeroOrOneOne\n",
    "\n",
    "operation ZeroZeroOrOneOne (qs : Qubit[]) : Int {\n",
    "    // ...\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Can't come up with a solution? See the explained solution in the [Measurements Workbook](./Workbook_Measurements.ipynb#Task-1.5.-$|00\\rangle$-or-$|11\\rangle$?).*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Task 1.6. Distinguish four basis states.\n",
    "\n",
    "**Input:** Two qubits (stored in an array of length 2) which are guaranteed to be in one of the four basis states ($|00\\rangle$, $|01\\rangle$, $|10\\rangle$, or $|11\\rangle$).\n",
    "\n",
    "**Output:**\n",
    "\n",
    "* 0 if the qubits were in the $|00\\rangle$ state,\n",
    "* 1 if they were in the $|01\\rangle$ state, \n",
    "* 2 if they were in the $|10\\rangle$ state, \n",
    "* 3 if they were in the $|11\\rangle$ state.\n",
    "\n",
    "In this task and the subsequent ones the order of qubit states in task description matches the order of qubits in the array (i.e., $|10\\rangle$ state corresponds to `qs[0]` in state $|1\\rangle$ and `qs[1]` in state $|0\\rangle$).\n",
    "\n",
    "The state of the qubits at the end of the operation does not matter."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%kata T106_BasisStateMeasurement\n",
    "\n",
    "operation BasisStateMeasurement (qs : Qubit[]) : Int {\n",
    "    // ...\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Can't come up with a solution? See the explained solution in the [Measurements Workbook](./Workbook_Measurements.ipynb#Task-1.6.-Distinguish-four-basis-states.).*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Task 1.7. Distinguish two basis states given by bit strings\n",
    "\n",
    "**Inputs:** \n",
    "\n",
    "1. $N$ qubits (stored in an array of length $N$) which are guaranteed to be in one of the two basis states described by the given bit strings.\n",
    "2. Two bit strings represented as `Bool[]`s.\n",
    "\n",
    "**Output:** \n",
    "\n",
    "* 0 if the qubits were in the basis state described by the first bit string,\n",
    "* 1 if they were in the basis state described by the second bit string.\n",
    "\n",
    "Bit values `false` and `true` correspond to $|0\\rangle$ and $|1\\rangle$ states. You are guaranteed that both bit strings have the same length as the qubit array, and that the bit strings differ in at least one bit.\n",
    "\n",
    "**You can use exactly one measurement.** The state of the qubits at the end of the operation does not matter.\n",
    "\n",
    "> Example:  for bit strings `[false, true, false]` and `[false, false, true]` return 0 corresponds to state $|010\\rangle$, and return 1 corresponds to state $|001\\rangle$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%kata T107_TwoBitstringsMeasurement\n",
    "\n",
    "operation TwoBitstringsMeasurement (qs : Qubit[], bits1 : Bool[], bits2 : Bool[]) : Int {\n",
    "    // ...\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Can't come up with a solution? See the explained solution in the [Measurements Workbook](./Workbook_Measurements.ipynb#Task-1.7.-Distinguish-two-basis-states-given-by-bit-strings).*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Task 1.8. Distinguish two superposition states given by two arrays of bit strings - 1 measurement\n",
    "\n",
    "**Inputs:** \n",
    "\n",
    "1. $N$ qubits (stored in an array of length $N$) which are guaranteed to be in one of the two superposition states described by the given arrays of bit strings.\n",
    "2. Two arrays of bit strings represented as `Bool[][]`s.  \n",
    "The arrays have dimensions $M_1 \\times N$ and $M_2 \\times N$ respectively, where $N$ is the number of qubits and $M_1$ and $M_2$ are the numbers of bit strings in each array. Note that in general $M_1 \\neq M_2$.  \n",
    "An array of bit strings `[b₁, ..., bₘ]` defines a state that is an equal superposition of all basis states defined by bit strings $b_1, ..., b_m$.  \n",
    "For example, an array of bit strings `[[false, true, false], [false, true, true]]` defines a superposition state $\\frac{1}{\\sqrt2}\\big(|010\\rangle + |011\\rangle\\big)$.\n",
    "\n",
    "You are guaranteed that there exists an index of a qubit Q for which: \n",
    " - all the bit strings in the first array have the same value in this position (all `bits1[j][Q]` are the same),\n",
    " - all the bit strings in the second array have the same value in this position (all `bits2[j][Q]` are the same),\n",
    " - these values are different for the first and the second arrays.\n",
    "\n",
    "> For example, for arrays `[[false, true, false], [false, true, true]]` and `[[true, false, true], [false, false, true]]` return 0 corresponds to state $\\frac{1}{\\sqrt2}\\big(|010\\rangle + |011\\rangle\\big)$, return 1 - to state $\\frac{1}{\\sqrt2}\\big(|101\\rangle + |001\\rangle\\big)$, and you can distinguish these states perfectly by measuring the second qubit.\n",
    "\n",
    "**Output:** \n",
    "\n",
    "* 0 if qubits were in the superposition state described by the first array,\n",
    "* 1 if they were in the superposition state described by the second array.\n",
    "\n",
    "**You are allowed to use exactly one measurement.**\n",
    "The state of the qubits at the end of the operation does not matter."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%kata T108_SuperpositionOneMeasurement\n",
    "\n",
    "operation SuperpositionOneMeasurement (qs : Qubit[], bits1 : Bool[][], bits2 : Bool[][]) : Int {\n",
    "    // ...\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Can't come up with a solution? See the explained solution in the [Measurements Workbook](./Workbook_Measurements.ipynb#Task-1.8.-Distinguish-two-superposition-states-given-by-two-arrays-of-bit-strings---1-measurement).*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Task 1.9. Distinguish two superposition states given by two arrays of bit strings\n",
    "\n",
    "**Inputs:** \n",
    "\n",
    "1. $N$ qubits (stored in an array of length $N$) which are guaranteed to be in one of the two superposition states described by the given arrays of bit strings.\n",
    "2. Two arrays of bit strings represented as `Bool[][]`s.  \n",
    "The arrays describe the superposition states in the same way as in the previous task,\n",
    "i.e. they have dimensions $M_1 \\times N$ and $M_2 \\times N$ respectively, $N$ being the number of qubits.\n",
    "\n",
    "The only constraint on the bit strings is that **all bit strings in the two arrays are distinct**. \n",
    "\n",
    "> Example:  for bit strings `[[false, true, false], [false, false, true]]` and `[[true, true, true], [false, true, true]]` return 0 corresponds to state $\\frac{1}{\\sqrt2}\\big(|010\\rangle + |001\\rangle\\big)$, return 1 to state $\\frac{1}{\\sqrt2}\\big(|111\\rangle + |011\\rangle\\big)$.\n",
    "\n",
    "**Output:** \n",
    "\n",
    "* 0 if qubits were in the superposition state described by the first array,\n",
    "* 1 if they were in the superposition state described by the second array.\n",
    "\n",
    "**You can use as many measurements as you wish.**\n",
    "The state of the qubits at the end of the operation does not matter."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%kata T109_SuperpositionMeasurement\n",
    "\n",
    "operation SuperpositionMeasurement (qs : Qubit[], bits1 : Bool[][], bits2 : Bool[][]) : Int {\n",
    "    // ...\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Can't come up with a solution? See the explained solution in the [Measurements Workbook](./Workbook_Measurements.ipynb#Task-1.9.-Distinguish-two-superposition-states-given-by-two-arrays-of-bit-strings).*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Task 1.10. $|0...0\\rangle$ state or W state ?\n",
    "\n",
    "**Input:** $N$ qubits (stored in an array of length $N$) which are guaranteed to be either in the $|0...0\\rangle$ state or in the [W state](https://en.wikipedia.org/wiki/W_state). \n",
    "\n",
    "**Output:**\n",
    "\n",
    "* 0 if the qubits were in the $|0...0\\rangle$ state,\n",
    "* 1 if they were in the W state.\n",
    "\n",
    "The state of the qubits at the end of the operation does not matter."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%kata T110_AllZerosOrWState\n",
    "\n",
    "operation AllZerosOrWState (qs : Qubit[]) : Int {\n",
    "    // ...\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Can't come up with a solution? See the explained solution in the [Measurements Workbook](./Workbook_Measurements.ipynb#Task-1.10.-$|0...0\\rangle$-state-or-W-state-?).*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Task 1.11. GHZ state or W state ?\n",
    "\n",
    "**Input:** $N \\ge 2$ qubits (stored in an array of length $N$) which are guaranteed to be either in the [GHZ state](https://en.wikipedia.org/wiki/Greenberger%E2%80%93Horne%E2%80%93Zeilinger_state) or in the [W state](https://en.wikipedia.org/wiki/W_state).\n",
    "\n",
    "**Output:** \n",
    "\n",
    "* 0 if the qubits were in the GHZ state,\n",
    "* 1 if they were in the W state.\n",
    "\n",
    "The state of the qubits at the end of the operation does not matter."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%kata T111_GHZOrWState\n",
    "\n",
    "operation GHZOrWState (qs : Qubit[]) : Int {\n",
    "    // ...\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Can't come up with a solution? See the explained solution in the [Measurements Workbook](./Workbook_Measurements.ipynb#Task-1.11.-GHZ-state-or-W-state-?).*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Task 1.12. Distinguish four Bell states.\n",
    "\n",
    "**Input:** Two qubits (stored in an array of length 2) which are guaranteed to be in one of the four Bell states.\n",
    "\n",
    "**Output:**\n",
    "\n",
    "* 0 if they were in the state $|\\Phi^{+}\\rangle = \\frac{1}{\\sqrt{2}} \\big(|00\\rangle + |11\\rangle\\big)$,\n",
    "* 1 if they were in the state $|\\Phi^{-}\\rangle = \\frac{1}{\\sqrt{2}} \\big(|00\\rangle - |11\\rangle\\big)$,\n",
    "* 2 if they were in the state $|\\Psi^{+}\\rangle = \\frac{1}{\\sqrt{2}} \\big(|01\\rangle + |10\\rangle\\big)$,\n",
    "* 3 if they were in the state $|\\Psi^{-}\\rangle = \\frac{1}{\\sqrt{2}} \\big(|01\\rangle - |10\\rangle\\big)$.\n",
    "\n",
    "The state of the qubits at the end of the operation does not matter."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%kata T112_BellState\n",
    "\n",
    "operation BellState (qs : Qubit[]) : Int {\n",
    "    // ...\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Can't come up with a solution? See the explained solution in the [Measurements Workbook](./Workbook_Measurements.ipynb#Task-1.12.-Distinguish-four-Bell-states.).*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Task 1.13. Distinguish four orthogonal 2-qubit states.\n",
    "\n",
    "**Input:** Two qubits (stored in an array of length 2) which are guaranteed to be in one of the four orthogonal states.\n",
    "\n",
    "**Output:**\n",
    "\n",
    "* 0 if they were in the state $|S_0\\rangle = \\frac{1}{2} \\big(|00\\rangle + |01\\rangle + |10\\rangle + |11\\rangle\\big)$,\n",
    "* 1 if they were in the state $|S_1\\rangle = \\frac{1}{2} \\big(|00\\rangle - |01\\rangle + |10\\rangle - |11\\rangle\\big)$,\n",
    "* 2 if they were in the state $|S_2\\rangle = \\frac{1}{2} \\big(|00\\rangle + |01\\rangle - |10\\rangle - |11\\rangle\\big)$,\n",
    "* 3 if they were in the state $|S_3\\rangle = \\frac{1}{2} \\big(|00\\rangle - |01\\rangle - |10\\rangle + |11\\rangle\\big)$.\n",
    "\n",
    "The state of the qubits at the end of the operation does not matter."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%kata T113_TwoQubitState\n",
    "\n",
    "operation TwoQubitState (qs : Qubit[]) : Int {\n",
    "    // ...\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Can't come up with a solution? See the explained solution in the [Measurements Workbook](./Workbook_Measurements.ipynb#Task-1.13.-Distinguish-four-orthogonal-2-qubit-states.).*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Task 1.14*. Distinguish four orthogonal 2-qubit states, part 2.\n",
    "\n",
    "**Input:** Two qubits (stored in an array of length 2) which are guaranteed to be in one of the four orthogonal states.\n",
    "\n",
    "**Output:**\n",
    "\n",
    "* 0 if they were in the state $|S_0\\rangle = \\frac{1}{2} \\big(+ |00\\rangle - |01\\rangle - |10\\rangle - |11\\rangle\\big)$,\n",
    "* 1 if they were in the state $|S_1\\rangle = \\frac{1}{2} \\big(- |00\\rangle + |01\\rangle - |10\\rangle - |11\\rangle\\big)$,\n",
    "* 2 if they were in the state $|S_2\\rangle = \\frac{1}{2} \\big(- |00\\rangle - |01\\rangle + |10\\rangle - |11\\rangle\\big)$,\n",
    "* 3 if they were in the state $|S_3\\rangle = \\frac{1}{2} \\big(- |00\\rangle - |01\\rangle - |10\\rangle + |11\\rangle\\big)$.\n",
    "\n",
    "The state of the qubits at the end of the operation does not matter."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%kata T114_TwoQubitStatePartTwo\n",
    "\n",
    "operation TwoQubitStatePartTwo (qs : Qubit[]) : Int {\n",
    "    // ...\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<i>Can't come up with a solution? See the explained solution in the [Measurements Workbook](./Workbook_Measurements.ipynb#Task-1.14*.-Distinguish-four-orthogonal-2-qubit-states,-part-2.).</i>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Task 1.15**. Distinguish two orthogonal states on three qubits.\n",
    "\n",
    "**Input:** Three qubits (stored in an array of length 3) which are guaranteed to be in one of the two orthogonal states.\n",
    "\n",
    "**Output:**\n",
    "\n",
    "* 0 if they were in the state $|S_0\\rangle = \\frac{1}{\\sqrt{3}} \\big(|100\\rangle + \\omega |010\\rangle + \\omega^2 |001\\rangle \\big)$,\n",
    "* 1 if they were in the state $|S_1\\rangle = \\frac{1}{\\sqrt{3}} \\big(|100\\rangle + \\omega^2 |010\\rangle + \\omega |001\\rangle \\big)$.\n",
    "\n",
    "Here $\\omega = e^{2i \\pi/ 3}$.\n",
    "\n",
    "The state of the qubits at the end of the operation does not matter."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%kata T115_ThreeQubitMeasurement\n",
    "\n",
    "operation ThreeQubitMeasurement (qs : Qubit[]) : Int {\n",
    "    // ...\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<i>Can't come up with a solution? See the explained solution in the [Measurements Workbook](./Workbook_Measurements.ipynb#Task-1.15**.-Distinguish-two-orthogonal-states-on-three-qubits.).</i>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Part II*. Discriminating Nonorthogonal States\n",
    "\n",
    "Variations of quantum state discrimination tasks are covered in the paper [\"Quantum State Discrimination\"](https://arxiv.org/pdf/quant-ph/0010114.pdf).\n",
    "* Task 2.1 is an example of hypothesis testing for two pure states.\n",
    "* Task 2.2 is an example of unambiguous state discrimination. See also the paper [\"Unambiguous quantum measurement of nonorthogonal states\"](https://www.researchgate.net/publication/13375059_Unambiguous_quantum_measurement_of_nonorthogonal_states)\n",
    "  for further information and hints about how to implement the unambiguous measurements required for this task. \n",
    "* Task 2.3 is the so-called Wootters/Peres game. See the following three references for more information and in particular\n",
    "  the book [3, p. 287] for a nice description of the optimal POVM. \n",
    "\n",
    "  [1] A. Holevo, “Information-theoretical aspects of quantum measurement,” Problems of Information Transmission, vol. 9, no. 2, pp. 110–118 (1973)\n",
    "\n",
    "  [2] A. Peres and W. K. Wootters, “Optimal detection of quantum information,” Phys. Rev. Lett., vol. 66, pp. 1119-1122, Mar. 1991.\n",
    "\n",
    "  [3] A. Peres, “Quantum Theory: Concepts and Methods,” Kluwer Academic Publishers, 2002."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <a name=\"task-21\"></a>Task 2.1*. $|0\\rangle$ or $|+\\rangle$?\n",
    "\n",
    "**Input:** A qubit which is guaranteed to be in either the $|0\\rangle$ or the $|+\\rangle$ state.\n",
    "\n",
    "**Output:**  `true` if the qubit was in the $|0\\rangle$ state, or `false` if it was in the $|+\\rangle$ state. The state of the qubit at the end of the operation does not matter.\n",
    "\n",
    "In this task your solution will be called multiple times, with one of the states picked with equal probability every time. You have to get overall accuracy of at least 80%.\n",
    "\n",
    "> This task is an example of quantum hypothesis testing, or state discrimination with minimum error."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%kata T201_IsQubitZeroOrPlus\n",
    "\n",
    "operation IsQubitPlusOrZero (q : Qubit) : Bool {\n",
    "    // ...\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Can't come up with a solution? See the explained solution in the [Measurements Workbook](./Workbook_Measurements_Part2.ipynb#task-21).*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <a name=\"task-22\"></a>Task 2.2**. $|0\\rangle$, $|+\\rangle$ or inconclusive?\n",
    "\n",
    "**Input:** A qubit which is guaranteed to be in either the $|0\\rangle$ or the $|+\\rangle$ state.\n",
    "\n",
    "**Output:** \n",
    "\n",
    "* 0 if the qubit was in the $|0\\rangle$ state, \n",
    "* 1 if it was in the $|+\\rangle$ state,\n",
    "* -1 if you can't decide, i.e., an \"inconclusive\" result. \n",
    "\n",
    "Your solution:\n",
    "\n",
    "* should never give 0 or 1 answer incorrectly (i.e., identify $|0\\rangle$ as 1 or $|+\\rangle$ as 0),\n",
    "* will be called multiple times, with one of the states picked with equal probability every time,\n",
    "* may give an inconclusive (-1) answer in at most 80% of all the cases,\n",
    "* must correctly identify the $|0\\rangle$ state as 0 in at least 10% of all the cases,\n",
    "* must correctly identify the $|1\\rangle$ state as 1 in at least 10% of all the cases.\n",
    "\n",
    "The state of the qubit at the end of the operation does not matter.\n",
    "\n",
    "> This task is an example of unambiguous state discrimination.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%kata T202_IsQubitZeroOrPlusSimpleUSD\n",
    "\n",
    "operation IsQubitPlusZeroOrInconclusiveSimpleUSD (q : Qubit) : Int {\n",
    "    // ...\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Can't come up with a solution? See the explained solution in the [Measurements Workbook](./Workbook_Measurements_Part2.ipynb#task-22).*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <a name=\"peres-wooters-game\"></a>Task 2.3**.  Peres/Wooters game\n",
    "\n",
    "**Input:** A qubit which is guaranteed to be in one of the three states:\n",
    "\n",
    "* $|A\\rangle = \\frac{1}{\\sqrt{2}} \\big( |0\\rangle + |1\\rangle \\big)$,\n",
    "* $|B\\rangle = \\frac{1}{\\sqrt{2}} \\big( |0\\rangle + \\omega |1\\rangle \\big)$,\n",
    "* $|C\\rangle = \\frac{1}{\\sqrt{2}} \\big( |0\\rangle + \\omega^2 |1\\rangle \\big)$,\n",
    "\n",
    "Here $\\omega = e^{2i \\pi/ 3}$.\n",
    "\n",
    "**Output:** \n",
    "\n",
    "* 1 or 2 if the qubit was in the $|A\\rangle$ state, \n",
    "* 0 or 2 if the qubit was in the $|B\\rangle$ state, \n",
    "* 0 or 1 if the qubit was in the $|C\\rangle$ state.\n",
    "\n",
    "You are never allowed to give an incorrect answer. Your solution will be called multiple times, with one of the states picked with equal probability every time.\n",
    "\n",
    "The state of the qubit at the end of the operation does not matter. \n",
    "<br/>\n",
    "<details>\n",
    "  <summary><b>Need a hint? Click here</b></summary>\n",
    "  You can use extra qubit(s) in your solution.\n",
    "</details>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%kata T203_IsQubitNotInABC\n",
    "\n",
    "operation IsQubitNotInABC (q : Qubit) : Int {\n",
    "    // ...\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Can't come up with a solution? See the explained solution in the [Measurements Workbook](./Workbook_Measurements_Part2.ipynb#peres-wooters-game).*"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Q#",
   "language": "qsharp",
   "name": "iqsharp"
  },
  "language_info": {
   "file_extension": ".qs",
   "mimetype": "text/x-qsharp",
   "name": "qsharp",
   "version": "0.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
